<template>
    <div class="_edit">
        <page-main class="_main">
            <!-- 返回 -->
            <el-button
                type="primary"
                icon="el-icon-d-arrow-left"
                @click="_return"
            >
                返回
            </el-button>
            <!-- 数据列表 -->
            <el-form :model="form" label-position="top">
                <el-form-item label="用户昵称">
                    <el-input v-model="form.nickName" />
                </el-form-item>
                <el-form-item label="用户头像">
                    <!-- 用户头像上传 -->
                    <el-upload
                        v-model="form.avatarUrl"
                        class="upload-demo"
                        drag
                        :limit="1"
                        :on-change="fileListfunc"
                        :file-list="fileList"
                        :auto-upload="false"
                        action=""
                        multiple
                        list-type="picture"
                    >
                        <i class="el-icon-upload" />
                        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
                        <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                    </el-upload>
                    <el-image
                        style="width: 100px; height: 100px;"
                        :src="form.avatarUrl"
                        fit="contain"
                    />
                </el-form-item>
                <!-- 累计积分 -->
                <el-form-item label="累计积分">
                    <el-input v-model="form.allintegral" type="text" />
                </el-form-item>
                <!-- 当前积分 -->
                <el-form-item label="当前积分">
                    <el-input v-model="form.integral" type="text" />
                </el-form-item>
                <!-- openid -->
                <el-form-item label="openid">
                    <el-input v-model="form.openid" type="text" />
                </el-form-item>
                <!-- 最近登录时间 -->
                <el-form-item label="最近登录时间" type="text">
                    <el-date-picker
                        v-model="form.logtime"
                        type="datetime"
                        placeholder="选择日期时间"
                        align="right"
                        :picker-options="pickerOptions"
                        format="yyyy-MM-dd HH:mm:ss"
                        value-format="timestamp"
                    />
                </el-form-item>
                <!-- 地址 -->
                <el-form-item label="地址">
                    <el-input v-model="form.company" type="text" />
                </el-form-item>
                <!-- 审核状态 -->
                <el-form-item label="审核状态">
                    <el-select v-model="form.ck" placeholder="请选择">
                        <el-option
                            v-for="item in options"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </el-form-item>
                <!-- 邮箱 -->
                <el-form-item label="邮箱">
                    <el-input v-model="form.email" type="text" />
                </el-form-item>
                <!-- 伙伴 -->
                <el-form-item label="伙伴">
                    <el-input v-model="form.partners" type="text" />
                </el-form-item>
                <!-- 手机号 -->
                <el-form-item label="手机号">
                    <el-input v-model="form.phone" type="text" />
                </el-form-item>
                <!-- 邮箱认证 -->
                <el-form-item label="邮箱认证">
                    <el-select v-model="form.emailreg" placeholder="请选择">
                        <el-option
                            v-for="item in options2"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </el-form-item>
            </el-form>

            <!-- 取消与立即修改 -->
            <el-row>
                <el-button @click="cancel">取消</el-button>
                <el-button type="primary" @click="upd()">立即修改</el-button>
            </el-row>
        </page-main>
    </div>
</template>
<script>

export default {
    data() {
        return {
            form: {
                nickName: '',
                avatarUrl: '',
                allintegral: '',
                integral: '',
                openid: '',
                logtime: '',
                company: '',
                ck: '',
                email: '',
                partners: '',
                phone: '',
                emailreg: ''
            },
            fileList: [],
            imgUrl: [],
            options2: [{
                value: true,
                label: '已认证'
            }, {
                value: false,
                label: '未认证'
            }
            ],
            // 审核状态
            ck: '',
            options: [{
                value: '1',
                label: '未通过'
            }, {
                value: '2',
                label: '已通过'
            }, {
                value: '3',
                label: '已离职'
            }, {
                value: '4',
                label: '审核中'
            }],
            // 最近登录时间
            logtime: '',
            pickerOptions: {
                disabledDate(time) {
                    return time.getTime() > Date.now()
                },
                shortcuts: [{
                    text: '今天',
                    onClick(picker) {
                        picker.$emit('pick', new Date())
                    }
                }, {
                    text: '昨天',
                    onClick(picker) {
                        const date = new Date()
                        date.setTime(date.getTime() - 3600 * 1000 * 24)
                        picker.$emit('pick', date)
                    }
                }, {
                    text: '一周前',
                    onClick(picker) {
                        const date = new Date()
                        date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
                        picker.$emit('pick', date)
                    }
                }]
            }

        }
    },
    created() {
        this.form.nickName = this.$route.params.nickName
        this.form.avatarUrl = this.$route.params.avatarUrl
        this.form.allintegral = this.$route.params.allintegral
        this.form.integral = this.$route.params.integral
        this.form.openid = this.$route.params.openid
        this.form.logtime = this.$route.params.logtime
        this.form.company = this.$route.params.company
        this.form.ck = this.$route.params.ck
        this.form.email = this.$route.params.email
        this.form.partners = this.$route.params.partners
        this.form.phone = this.$route.params.phone
        this.form.emailreg = this.$route.params.emailreg

    },
    methods: {
        // 获取当前年月日
        mounted() {
            var that = this
            let yy = new Date().getFullYear()
            let mm = new Date().getMonth() + 1
            let dd = new Date().getDate()
            let hh = new Date().getHours()
            let mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes()
            let ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds()
            that.gettime = yy + '-' + mm + '-' + dd + ' ' + hh + ':' + mf + ':' + ss
            // console.log(this.updateTime)
        },

        // 是否上架的回调
        changeStatus(res) {
            this.value = res
            console.log(res)
        },
        _return() {
            this.$router.go(-1)
        },
        async fileListfunc(file, fileList) {
            console.log('file', file, 'fileList', fileList)
            this.file = fileList[0].raw
            var flx = this.file.type.split('/')[1]
            var fileda =  await  this.$cloudbase
                .uploadFile({
                    cloudPath: 'user' + new Date().getTime() + '.' + flx,
                    filePath: this.file
                })
            this.form.avatarUrl = fileda.fileID

        },
        // 取消
        cancel() {
            this.$router.go(-1)
        },
        // 立即修改
        upd() {
            var that = this
            // 获取时间戳
            let timestamp = Date.parse(new Date())
            this.updateTime = timestamp
            var user = this.$cloudbase.database().collection('user')
            user.where({_id: this.$route.params.id}).update({
                nickName: that.form.nickName,
                avatarUrl: that.form.avatarUrl,
                allintegral: that.form.allintegral,
                integral: that.form.integral,
                openid: that.form.openid,
                logtime: that.form.logtime,
                company: that.form.company,
                ck: that.form.ck,
                email: that.form.email,
                partners: that.form.partners,
                phone: that.form.phone,
                emailreg: that.form.emailreg
            }).then(() => {
                this.$router.go(-1)
            }).catch(err => {
                console.log('修改失败' + err)
            })
        }

    }
}
</script>

<style>
._edit ._main .el-button {
    margin-bottom: 20px;
}
._edit ._main .fileId {
    margin-bottom: 20px;
}
._edit ._main .el-switch {
    margin-bottom: 20px;
}
</style>
